<template>
  <div class="ad-chart-w">
    <Title>
      <template #content>
        <p class="title">转化率漏洞</p>
      </template>
    </Title>
    <div class="flex" style="margin-top: 20px">
      <ul>
        <li class="all-target" v-for="(target, i) in allTargetKeys" :key="i">
          <p>
            <span>{{ target.name }}</span>
            <a-popover v-if="target.tip" :content="target.tip">
              <icon-question-circle />
            </a-popover>
          </p>
          <p class="value">
            <span class="value-price">{{ formatVal(target, 'value') }}%</span>
          </p>
          <div class="price-item flex-c">
            <span class="label">环比</span>
            <span class="val">{{ formatVal(target, 'monValue') }}%</span>
            <template v-if="Number($extract(targetMap[target.key], 'monComparedValue')) > 0">
              <icon-caret-up style="color: rgb(var(--success-5))" />
              <span class="diff" style="color: rgb(var(--success-5))">{{ $extract(targetMap[target.key], 'monComparedValue') }}%</span>
            </template>
            <template v-else>
              <icon-caret-down style="color: rgb(var(--danger-5))" />
              <span class="diff" style="color: rgb(var(--danger-5))">{{ $extract(targetMap[target.key], 'monComparedValue') }}%</span>
            </template>
          </div>
        </li>
      </ul>
      <div class="chart-w" ref="adFunnelChart" style="height: 380px"></div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import Title from '@/views/index/components/Title.vue'
  import useEcharts from '@/hooks/useEcharts'
  import { ref, nextTick, onBeforeUnmount } from 'vue'
  import { dispose } from 'echarts/core'
  const props = defineProps({
    searchParams: {
      type: Object,
      default: () => {},
    },
    targetMap: {
      type: Object,
      default: () => {},
    },
  })

  const targetList = ref<any>({
    ctr: {
      key: 'ctr',
      name: '广告点击率(CTR)',
      type: 'percentage',
      tip: '广告点击转化率=点击量/曝光量',
    },

    cvr: {
      key: 'cvr',
      name: '广告转化率',
      type: 'percentage',
      tip: '广告转化率=广告总订单量/点击量',
    },
  })
  const allTargetKeys = ref<Array<any>>(Object.values(targetList.value))

  // 指标金额
  function formatVal(item: any, key: string) {
    let val = props.targetMap[item.key]?.[key] || 0
    return Number(val).toFixed(2)
  }

  // 刷新数据
  async function refreshData() {
    nextTick(() => {
      initChart()
    })
  }

  const adFunnelChart = ref<HTMLDivElement | null>(null)

  function initChart() {
    // 容易导致重复渲染，先清除
    if (adFunnelChart.value) {
      dispose(adFunnelChart.value as HTMLDivElement)
    }
    const option = {
      series: [
        {
          name: 'Funnel',
          type: 'funnel',
          left: '10%',
          top: 0,
          bottom: 10,
          width: '80%',
          min: 0,
          max: 100,
          minSize: '0%',
          maxSize: '100%',
          sort: 'descending',
          gap: 2,
          label: {
            show: true,
            position: 'inside',
            fontSize: 14,
            formatter(v: any) {
              return `${v.data.name}\n${props.targetMap[v.data.key]?.value}`
            },
          },
          itemStyle: {
            borderColor: '#fff',
            borderWidth: 1,
          },
          emphasis: {
            label: {
              fontSize: 16,
            },
          },
          data: [
            { name: '曝光量', value: 100, key: 'impressions' },
            { name: '点击量', value: 80, key: 'clicks' },
            { name: '下单量', value: 60, key: 'adPurchasesOrder' },
          ],
        },
      ],
    }
    nextTick(() => useEcharts(adFunnelChart.value as HTMLDivElement).setOption(option))
  }
  onBeforeUnmount(() => {
    dispose(adFunnelChart.value as HTMLDivElement)
  })

  defineExpose({ refreshData })
</script>
<style lang="less" scoped>
  .ad-chart-w {
    margin-top: 10px;
    background-color: #fff;
    padding: 20px;
    position: relative;
  }
  .all-target {
    width: 170px;
    border: 1px solid var(--color-border);
    height: 80px;
    padding: 10px 5px 10px 10px;
    position: relative;
    font-size: 12px;
    line-height: 20px;
    margin-bottom: 10px;
    .value-price {
      font-size: 16px;
      font-weight: bold;
      margin-right: 8px;
    }
    .line {
      position: absolute;
      width: 100%;
      left: 0;
      top: 0;
      height: 2px;
    }
    .price-item {
      .label {
        margin-right: 5px;
      }
      .val {
        margin-right: 5px;
      }
      .arco-icon {
        font-size: 18px;
      }
    }
    .diff {
      margin-left: 5px;
    }
  }
  .type-w {
    position: absolute;
    top: 10px;
    right: 20px;
  }

  :deep(.arco-radio-group-button) {
    padding: 0;
    .arco-radio-button {
      margin: 0;
      background-color: #fff;
      line-height: 26px;
      border: 1px solid var(--color-border);
      &:first-child {
        border-radius: 4px 0 0 4px;
      }
      &:last-child {
        border-radius: 0 4px 4px 0;
      }
    }
    .arco-radio-checked {
      border-color: rgb(var(--primary-6));
    }
  }
  .chart-w {
    height: 350px;
    flex: 1;
    width: 100%;
  }
</style>
